package com.truyen.utils;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;


public class PagingTag extends TagSupport {
	static Logger logger = Logger.getLogger(PagingTag.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -3479891350802464345L;
	private PagingInfo pagingInf;
	private String searchUrl;
	private String curPageParam;
	private String searchValueParam;

	@Override
	public int doStartTag() throws JspException {
		
		try {
			JspWriter out = pageContext.getOut();
			String strTrangDau = new String("«");
			String strTrangTruoc = new String("‹");
			String strTrangSau = new String("›");
			String strTrangCuoi = new String("»");
			
			if (pagingInf.getTotalPage() > 1) {
				try {

					if (pagingInf.getCurrentPage() != 1) {
						out.write("<button class='bPage button blue small' value='1' title=\""+ strTrangDau +"\" ><span>"+ strTrangDau +"</span></button>");
					}

					if (pagingInf.getCurrentPage() <= pagingInf.getTotalPage()
							&& pagingInf.getCurrentPage() != 1) {
						out.write("<button class='bPage button blue small' value="
								+ (pagingInf.getCurrentPage() - 1)
								+ " title=\""
								+ strTrangTruoc
								+ "\" ><span>"+ strTrangTruoc+"</span></button>");
					}

					for (int i = pagingInf.getBegin(); i <= pagingInf.getEnd(); i++) {
						if (i == pagingInf.getCurrentPage()) {
							out.write("<button class='button small orange'><span>" + i
									+ "</span></button>");
						} else {
							out.write("<button class='bPage button blue small' value="+ i	+ " title='Trang " + i + "'><span>" + i + "</span></button>");
						}
					}

					if (pagingInf.getCurrentPage() < pagingInf.getTotalPage()) {
						out.write("<button class='bPage button blue small' value="+ (pagingInf.getCurrentPage() + 1) + " title=\"" + strTrangSau + "\"><span>" + strTrangSau + "</span></button>");
					}

					if (pagingInf.getCurrentPage() != pagingInf.getTotalPage()) {
						out.write("<button class='bPage button blue small' value="
								+ pagingInf.getTotalPage()
								+ " title=\""
								+ strTrangCuoi
								+ "\"><span>"
								+ strTrangCuoi
								+ "</span></button>");
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e1) {
			logger.error(e1.getMessage(), e1);
		}		

		return SKIP_BODY;
	}

	/**
	 * @param pagingInf
	 *            the pagingInf to set
	 */
	public void setPagingInf(PagingInfo pagingInf) {
		this.pagingInf = pagingInf;
	}

	/**
	 * @return the pagingInf
	 */
	public PagingInfo getPagingInf() {
		return pagingInf;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setSearchUrl(String url) {
		this.searchUrl = url;
	}

	/**
	 * @return the url
	 */
	public String getSearchUrl() {
		return searchUrl;
	}

	/**
	 * @param curPageParam
	 *            the curPageParam to set
	 */
	public void setCurPageParam(String curPageParam) {
		this.curPageParam = curPageParam;
	}

	/**
	 * @return the curPageParam
	 */
	public String getCurPageParam() {
		return curPageParam;
	}

	/**
	 * @param searchValueParam
	 *            the searchValueParam to set
	 */
	public void setSearchValueParam(String searchValueParam) {
		this.searchValueParam = searchValueParam;
	}

	/**
	 * @return the searchValueParam
	 */
	public String getSearchValueParam() {
		return searchValueParam;
	}
}
